/******************************************************************************
 * 
 *****************************************************************************/

package com.gw.business.system.semil.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gw.business.common.DwzAjaxRes;
import com.gw.business.system.semil.model.EmailSetModel;
import com.gw.platform.controller.DefaultPageController;
import com.gw.platform.model.UserModel;
import com.gw.platform.page.impl.Page;
import com.gw.platform.util.StringUtils;

/**
 * @author kidy
 * @since 1.0
 * @version 2015年1月27日 kidy
 */
@Controller
@RequestMapping("emailSet")
public class EmailSetController extends DefaultPageController {
    
    /**
     * 分页查询所有信息
     * 
     * @return
     */
    @RequestMapping("/page")
    public Callable<String> queryEmailSet() {
        return new Callable<String>() {
            
            public String call() throws Exception {
                String pageSize = request.getParameter("numPerPage");
                String pageNum = request.getParameter("pageNum");
                
                if (StringUtils.isEmpty(pageNum)) {
                    pageNum = "1";
                    pageSize = Page.DEFAULT_PAGE_SIZE + "";
                    
                }
                // 设置当前页
                setCurrent(Long.parseLong(pageNum));
                String userName = request.getParameter("userName");
                String email=request.getParameter("email");
                Map<String, Object> map = new HashMap<String, Object>();
                if (userName!=null&&userName!="") {
                    map.put("userName", userName);
                  
                }
                if(email!=null||email!=""){
                    map.put("email", email);
                }
                page = defaultService.page("emailSet.page", "emailSet.total", getCurrent(), map, Integer.parseInt(pageSize));
                // 设置分页操作的url
                request.setAttribute("page", page);
                request.setAttribute("userName", userName);
                request.setAttribute("email", email);
                return "system/semil/page";
            }
        };
    }
    
    /**
     * 跳转到添加页面
     * 
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public String create() {
        
        return "system/semil/create";
    }
    
    /**
     * 跳转到加载用户页面
     * 
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public Callable<String> update() {
        return new Callable<String>() {
            
            public String call() throws Exception {
                EmailSetModel emailSetModel = new EmailSetModel();
                emailSetModel.setId(Integer.parseInt(request.getParameter("uId")));
                
                request.setAttribute("emailSetModel", defaultService.load("emailSet.loadEmailSet", emailSetModel));
                
                return "system/semil/update";
            }
        };
    }
    
    /**
     * 添加系统用户
     * 
     * @return
     */
    @RequestMapping("/updateSave")
    @ResponseBody

    public Callable<Object> updateSave(final EmailSetModel emailSetModel) {
        return new Callable<Object>() {
            
            public Object call() throws Exception {
                String callbackType = request.getParameter("callbackType");
                String navTabId = request.getParameter("navTabId");
                
                DwzAjaxRes res = new DwzAjaxRes();
                res.setCallbackType(callbackType);
                res.setNavTabId(navTabId);
                if(emailSetModel.getErrorType().indexOf(",")>0){
                    emailSetModel.setErrorType("2");
                }
               Map mobleMap = (Map) defaultService.load("emailSet.loadEmailSetByMobile", emailSetModel);
                Map emailMap = (Map) defaultService.load("emailSet.loadEmailSetByEmail", emailSetModel);
                if (mobleMap != null || emailMap != null) {
                    if (mobleMap != null) {
                        res.setStatusCode(DwzAjaxRes.STATUSCODE_300);
                        res.setMessage("手机号已经存在！");
                    } else if (emailMap != null) {
                        res.setStatusCode(DwzAjaxRes.STATUSCODE_300);
                        res.setMessage("邮箱已经存在！");
                    }
                } else {
                    defaultService.update("emailSet.updateEmailSet", emailSetModel);
                }
                
                return res;
            }
        };
    }
    
    /**
     * 删除
     * 
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Callable<Object> delete(final UserModel user) {
        return new Callable<Object>() {
            
            public Object call() throws Exception {
                String callbackType = request.getParameter("callbackType");
                String navTabId = request.getParameter("navTabId");
                DwzAjaxRes res = new DwzAjaxRes();
                res.setCallbackType(callbackType);
                res.setNavTabId(navTabId);
                defaultService.delete("emailSet.deleteEmailSet", Integer.parseInt(request.getParameter("uId")));
                
                return res;
            }
        };
    }
    
    /**
     * 添加系统用户
     * 
     * @return
     */
    @RequestMapping("/insert")
    @ResponseBody
    public Callable<Object> insert(final EmailSetModel emailSetModel) {
        return new Callable<Object>() {
            
            public Object call() throws Exception {
                String callbackType = request.getParameter("callbackType");
                String navTabId = request.getParameter("navTabId");
                DwzAjaxRes res = new DwzAjaxRes();
                res.setCallbackType(callbackType);
                res.setNavTabId(navTabId);
                emailSetModel.setCreateTime(new Date());
                if(emailSetModel.getErrorType().indexOf(",")>0){
                    emailSetModel.setErrorType("2");
                }
                
                Map mobleMap = (Map) defaultService.load("emailSet.loadEmailSetByMobile", emailSetModel);
                Map emailMap = (Map) defaultService.load("emailSet.loadEmailSetByEmail", emailSetModel);
                if (mobleMap != null || emailMap != null) {
                    if (mobleMap != null) {
                        res.setStatusCode(DwzAjaxRes.STATUSCODE_300);
                        res.setMessage("手机号已经存在！");
                    } else if (emailMap != null) {
                        res.setStatusCode(DwzAjaxRes.STATUSCODE_300);
                        res.setMessage("邮箱已经存在！");
                    }
                } else {
                    defaultService.insert("emailSet.insertEmailSet", emailSetModel);
                }
                
               
                return res;
            }
        };
    }
}
